from building import *
import os
import zipfile

user_libraries_root_path = GetCurrentDir()
# scripts_path = os.path.join(user_libraries_root_path, '..', '..', 'scripts')
# sys.path.append(scripts_path)

# run ALM script
# alm_path = os.path.join(scripts_path, 'ALM')
# sys.path.append(alm_path)
# from ALM_CMD import ALM_Init
# ALM_Init(alm_path)

src = Glob('*.c') + Glob('*.cpp')
inc = [user_libraries_root_path]

# check if .h or .hpp files exsit
def check_h_hpp_exsit(path):
    file_dirs = os.listdir(path)
    for file_dir in file_dirs:
        if os.path.splitext(file_dir)[1] in ['.h', '.hpp']:
            return True
    return False

# Find and deal the ZIP file first
files = os.listdir(user_libraries_root_path)
for file in files:
    if os.path.isfile(file) == True:
        splitext = os.path.splitext(file)
        if splitext[1] == '.zip':
            # unzip the file
            zFile = zipfile.ZipFile(os.path.join(user_libraries_root_path, file), 'r')
            for fileM in zFile.namelist():
                zFile.extract(fileM, '.')
            zFile.close()
            os.remove(file) # delete the zip file

# Add all C/C++ and header files at 'user' folder into the group
for root, dirs, files in os.walk(user_libraries_root_path):
    for dir in dirs:
        current_path = os.path.join(root,dir)

        # exclude 'examples' folder
        if 'examples' in current_path: 
            continue

        # add all .c/cpp file and .h/hpp file
        src = src + Glob(os.path.join(current_path, '*.c'))
        src = src + Glob(os.path.join(current_path, '*.cpp'))
        if check_h_hpp_exsit(current_path):
            inc = inc + [current_path]

# Add all Arduino libraries at the root of 'package' folder into the group
package_root_path = os.path.join(user_libraries_root_path, '..', '..','..')
file_dirs = os.listdir(package_root_path)
for file_dir in file_dirs:
    current_path = os.path.join(package_root_path, file_dir)

    # Arduino libraries must have the 'library.properties' file.
    # If this folder contains SConscript, just follow the SConscript rule rather than the Arduino rule.
    if os.path.isdir(os.path.abspath(current_path)) == False or \
       os.path.exists(os.path.join(current_path, 'library.properties')) == False or \
       os.path.exists(os.path.join(current_path, 'SConscript')) == True:
        continue

    # Now, we have found the Arduino library folder

    # add all .c/cpp file and .h/hpp file
    src = src + Glob(os.path.join(current_path, '*.c'))
    src = src + Glob(os.path.join(current_path, '*.cpp'))
    if check_h_hpp_exsit(current_path):
        inc = inc + [current_path]

    # find the current folder's sub-folders and sub-files recursively
    for root, dirs, files in os.walk(current_path):
        for dir in dirs:
            current_sub_path = os.path.join(root,dir)

            # exclude 'examples' folder
            if 'examples' in current_sub_path:
                continue

            # add all .c/cpp file and .h/hpp file
            src = src + Glob(os.path.join(current_sub_path, '*.c'))
            src = src + Glob(os.path.join(current_sub_path, '*.cpp'))
            if check_h_hpp_exsit(current_sub_path):
                inc = inc + [current_sub_path]

group = DefineGroup('Arduino-libraries', src, depend = ['PKG_USING_RTDUINO'], CPPPATH = inc)

Return('group')
